<template>
  <!--引入container布局-->
    <el-container class="home-container">
      <!--头部-->
      <el-header>
        <div>
          <img src="../assets/logo.png" alt/>
          <span>学生学籍管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!--主体-->
      <el-container>
        <!--侧边栏-->
        <el-aside width="isCollapse?'64px':'250px'">
          <div class="toggle-button" @click="toggleCollapase"> ||| </div>
          <el-menu background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" unique-opened :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
            <!--一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.title}}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="it.path" v-for="it in item.sList" :key="it.id" @click="saveNavState(it.path)">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{it.title}}</span>
              </template>
            </el-menu-item>


            </el-submenu>

          </el-menu>
        </el-aside>
        <!--主体内容-->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  data(){
     return{
       //菜单列表
       menuList:[],
       isCollapse:false, //伸缩
       activePath:'/welcome',
     }
  },
  //加载事件
  created(){
    //查询menuList
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath"); //取出session的path动态修改
  },
  methods:{
    logout(){
      //清除session数据
      window.sessionStorage.clear();
      //安全退出回到登录页面
      this.$router.push("/login");
    },
    //获取导航菜单方法
     async getMenuList(){
      const {data:res} = await this.$http.get("menus");
      console.log(res);
      if(res.flag != 200) return this.$message.error("获取列表失败");
      this.menuList = res.menus; //访问成功数据回填
     },
      toggleCollapase(){
        this.isCollapse = !this.isCollapse;
      },
      //保存路径
    saveNavState(activePath){
      //存放在session里
      window.sessionStorage.setItem('activePath',activePath);
      this.activePath = activePath;
    }
  }
}
</script>

<style lang="less" scoped>

.home-container{
  height: 100%;
}

.el-header{
  background-color: darkcyan;
  display: flex;
  justify-content: space-between;
  padding-left: 0%;
  align-items: center;
  color: white;
  font-size: 20px;
  div{
    display: flex;
    align-items: center;
    span{
    margin-left: 15px;
  }
  }

}

.el-aside{
  background-color: lightseagreen;
  .el-menu{
    border-right: none;
  }
}

.el-main{
  background-color: aliceblue;
}
img{
  width:70px;
  height:60px;
}

.toggle-button{
  background-color: gold;
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer; //小手
}
</style>
